package com.santiao.publics.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

//import com.santiao.youxiang.BloodglucoseApplication;
//import com.santiao.youxiang.CalculatorApplication;
import com.santiao.publics.bean.PendingEventBean;
import com.santiao.publics.config.Const;
import com.santiao.publics.config.Logger;
import com.santiao.youxiang.ShuidianApplication;

public class SharedPreferencesUtil {

    public static  String IF_AD="IF_AD";
    public static  String IF_AD_2="IF_AD_2";//自然流量广告模式
    public static String SHARED_KEY = Const.TAG+"_KEY";
    public static String SHARED_KEY_AD = Const.TAG+"_KEY_AD";
    public static String SHARED_KEY_ID = Const.TAG+"_KEY_ID";
    public static String SHARED_KEY_STATUS = Const.TAG+"_KEY_STATUS";

    public static String FIRST_RUN_TIME = "FIRST_RUN_TIME";
    public static String EVENT = "EVENT";
    public static String FUNCTION = "FUNCTION";
    public static String OTHER = "OTHER";


    public static String AGREEMENT_STATUS = "AGREEMENT_STATUS";
    public static String CATCH_TIME = "CATCH_TIME";

    public static String SettingPersonalization = "SettingPersonalization";

    public static String FIRST_HABIT = "0";

    @SuppressLint("CommitPrefEdits")
    public static void setFirstHabit(long time) {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        sharedPreferences.edit().putLong(FIRST_HABIT, time).apply();
    }


    @SuppressLint("CommitPrefEdits")
    public static long getFirstHabit() {
        return ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE).getLong(FIRST_HABIT, 0);
    }

    @SuppressLint("CommitPrefEdits")
    public static void setTimeMillis() {
        long currentTimeMillis = System.currentTimeMillis();
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        sharedPreferences.edit().putLong(CATCH_TIME, currentTimeMillis).apply();
    }


    @SuppressLint("CommitPrefEdits")
    public static long getTimeMillis() {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        return sharedPreferences.getLong(CATCH_TIME, 0);
    }


    @SuppressLint("CommitPrefEdits")
    public static void saveFirstRunTime(long time) {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        sharedPreferences.edit().putLong(FIRST_RUN_TIME, time).apply();
    }


    @SuppressLint("CommitPrefEdits")
    public static long getFirstRunTime() {
        return ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE).getLong(FIRST_RUN_TIME, 0);
    }


    @SuppressLint("CommitPrefEdits")
    public static void saveAgreementStatus(boolean status) {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(AGREEMENT_STATUS, status).apply();
    }


    @SuppressLint("CommitPrefEdits")
    public static boolean getAgreementStatus() {
        return ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE).getBoolean(AGREEMENT_STATUS, false);
    }

    @SuppressLint("CommitPrefEdits")
    public static void saveStatus(String EventName, boolean pendingStatus) {
        Logger.i(Const.TAG,"saveStatus----------------"+EventName+"-----------"+pendingStatus);
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_AD, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(EventName, pendingStatus).apply();
    }

    @SuppressLint("CommitPrefEdits")
    public static boolean getStatus(String keyName) {
        boolean bo = false;
      SharedPreferences sharedPreferences =   ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_AD, Context.MODE_PRIVATE);
        if (sharedPreferences.contains(keyName)) {
              bo = sharedPreferences.getBoolean(keyName, false);
        }
        Logger.i(Const.TAG,"getStatus----------------"+keyName+"-----------"+bo);
        return bo;
    }


    @SuppressLint("CommitPrefEdits")
    public static void saveString(String EventName, String value) {
        Logger.i(Const.TAG,"saveString----------------"+EventName+"-----------"+value);
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_ID, Context.MODE_PRIVATE);
        sharedPreferences.edit().putString(EventName, value).apply();
    }



    @SuppressLint("CommitPrefEdits")
    public static String getString(String keyName) {
        String bo = null;
        SharedPreferences sharedPreferences =   ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_ID, Context.MODE_PRIVATE);
        if (sharedPreferences.contains(keyName)) {
            bo = sharedPreferences.getString(keyName, null);
        }
        Logger.i(Const.TAG,"getString----------------"+keyName+"-----------"+bo);
        return bo;
    }

    @SuppressLint("CommitPrefEdits")
    public static void saveStatusCount(String EventName, float pendingStatus) {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_STATUS, Context.MODE_PRIVATE);
        sharedPreferences.edit().putFloat(EventName, pendingStatus).apply();
    }

    @SuppressLint("CommitPrefEdits")
    public static float getStatusCount(String keyName) {
        return ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_STATUS, Context.MODE_PRIVATE).getFloat(keyName, 0);
    }

    @SuppressLint("CommitPrefEdits")
    public static void saveSettingPersonalization(boolean Status) {
        SharedPreferences sharedPreferences = ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        sharedPreferences.edit().putBoolean(SharedPreferencesUtil.SettingPersonalization, Status).apply();
    }

    @SuppressLint("CommitPrefEdits")
    public static boolean getSettingPersonalization() {
        return ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE).getBoolean(SharedPreferencesUtil.SettingPersonalization, false);
    }

    public static void saveObject(Context context, String key, PendingEventBean object) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
            objectOutputStream.writeObject(object);
            objectOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        byte[] serializedObject = outputStream.toByteArray();
        editor.putString(key, Base64.encodeToString(serializedObject, Base64.DEFAULT));
        editor.apply();
    }

    public static PendingEventBean loadObject(Context context, String key) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE);
        String serializedObjectString = sharedPreferences.getString(key, null);

        if (serializedObjectString != null) {
            byte[] serializedObject = Base64.decode(serializedObjectString, Base64.DEFAULT);
            ByteArrayInputStream inputStream = new ByteArrayInputStream(serializedObject);

            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
                PendingEventBean object = (PendingEventBean) objectInputStream.readObject();
                objectInputStream.close();
                return object;
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 清除缓存状态
     */
    @SuppressLint("CommitPrefEdits")
    public static void clearStatus() {
        ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY_STATUS, Context.MODE_PRIVATE).edit().clear().apply();
    }

    /**
     * 清除缓存状态
     */
    @SuppressLint("CommitPrefEdits")
    public static void clear_agreement_status() {
        ShuidianApplication.getInstance().getSharedPreferences(SHARED_KEY, Context.MODE_PRIVATE).edit().putBoolean(AGREEMENT_STATUS, false).apply();
    }
}
